<?php
/**
 -----------------------------
 *  功能:公共方法
 */
namespace Home\Model;
use Think\Model;

class PublicModel extends Model{
	public function sort($cate,$pid=0,$level=0,$html='--'){
		$tree=array();
		foreach($cate as $v){
			if($v['pid'] == $pid){
				$v['level'] = $level + 1;
				$v['html'] = str_repeat($html, $level);
				$tree[] = $v;
				$tree = array_merge($tree, self::sort($cate,$v['uid'],$level+1,$html));
			}
		}
		return $tree;
	}
	
	public function upload(){
		$upload=new \Think\Upload();// 实例化上传类
	    $upload->maxSize=3145728 ;// 设置附件上传大小
	    $upload->rootPath=$_SERVER['DOCUMENT_ROOT'].Public_upload ;//'./Public/Uploads/';
	    $upload->saveName = array('uniqid','');
	    $upload->exts=array('jpg', 'gif', 'png', 'jpeg');
	    $upload->autoSub=true;
	    $upload->subName=array('date','Ymd');
	    $info=$upload->upload();
        if(!$info){
            return array(1,$upload->getError());
        }else{
        	$image = new \Think\Image();
        	$image->open($upload->rootPath.$info['mypic']['savepath'].$info['mypic']['savename']);
            $image->thumb(100,100)->save($upload->rootPath.$info['mypic']['savepath']."small".$info['mypic']['savename']);
            return array(0,"上传成功！");
        }
	}
	//订单日志
	public function log(){		
		$list = M('order')->select();
		foreach($list as $val){
			$orderid[] = $val['orderid'];			
		}
		
		$log = M('log')->select(); 
		foreach($log as $v){
			$ord[] = $v['orderid'];
		}
		//相同部分
		$hh = array_intersect($ord,$orderid);
		
		if(count($log) == 0){
			foreach($list as $vv){
				$da = M('order')->where(array('orderid'=>$vv['orderid']))->find();
				$data['orderid'] = $da['orderid'];
				$data['times'] = $da['times'];
				$data['detail'] = $da['detail'];
				$data['uid'] = $da['uid'];
				$data['uname'] = $da['uname'];
				M('log')->data($data)->add();
				
			}
		}else{	
			foreach($list as $vv){
				if(!in_array($vv['orderid'],$hh)){
					$da = M('order')->where(array('orderid'=>$vv['orderid']))->find();
					$data['orderid'] = $da['orderid'];
					$data['times'] = $da['times'];
					$data['detail'] = $da['detail'];
					$data['uid'] = $da['uid'];
					$data['uname'] = $da['uname'];
					M('log')->data($data)->add();
				}
			}
		
		}
		
	}
		
		
		
	
	
	
	
	
	
}